//
//  ViewController.m
//  WayToHome
//
//  Created by A-01 on 2020/6/22.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "ViewController.h"
#import "LevelViewController.h"
#import "RecordViewController.h"
#import "IntroductionViewController.h"
#import "AppDelegate.h"

@interface ViewController ()

@property (nonatomic, weak)UILabel * titleLab;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, strong)UIButton * musicBtn;
@property (nonatomic, strong)UIButton * howBtn;
@property (nonatomic, strong)UIView * playView;
@property (nonatomic, strong)UIView * recordView;
@property (nonatomic, strong)UIView * instroView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.backButton.hidden = YES;
    self.bottomImageView.hidden = YES;
    [self setupUI];
}


- (void)setupUI{
    
    UILabel * titleLab = [UILabel new];
    
    titleLab.text = @"Home road";
    titleLab.textColor = [UIColor colorWithHexString:@"0AB966"];
    titleLab.font = [UIFont boldSystemFontOfSize:24];
    
    self.titleLab = titleLab;
    
    [self.view addSubview:self.titleLab];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"home"];
    
    self.rightImageView = rightImageView;
    
    [self.view addSubview:self.rightImageView];
    
    self.musicBtn = [self setupRightTopButtonWith:@"music"];
    [self.musicBtn setImage:[UIImage imageNamed:@"banMusic"] forState:UIControlStateSelected];
    [self.view addSubview:self.musicBtn];
    self.howBtn = [self setupRightTopButtonWith:@"how"];
    [self.view addSubview:self.howBtn];
    
    self.playView = [self setupViewWithTitle:@"Play" WithImage:@"leaf" WithIndex:901];
    [self.view addSubview:self.playView];
    self.recordView = [self setupViewWithTitle:@"Record" WithImage:@"leaf" WithIndex:902];
    [self.view addSubview:self.recordView];
    self.instroView = [self setupViewWithTitle:@"Instroduction" WithImage:@"flower" WithIndex:903];
    [self.view addSubview:self.instroView];
}


- (void)clickRightTopButton:(UIButton *)button{
    
    if (button == self.howBtn) {
        
        IntroductionViewController * introVC = [IntroductionViewController new];
        introVC.titleStr = @"How To Play";
        introVC.valueStr = @"In the game, you should remember the way home quickly, because the street lights will be off soon, you need to turn on the road grid according to the order, and finally return home.";
        [self.navigationController pushViewController:introVC animated:YES];
    }else{
        
        button.selected = !button.selected;
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if (!button.selected) {
            
            [appDelegate playMusic];
        }else{
            
            [appDelegate pluaseMusic];
        }
    }
}

//轻拍手势的回调方法
- (void)tapAction:(UITapGestureRecognizer*)sender{
    //可以根据手势得到它当前所作用的视图
    UIView * view = (UIView*)sender.view;
    if (view.tag == 901) {
        
        LevelViewController * levelVC = [LevelViewController new];
        
        [self.navigationController pushViewController:levelVC animated:YES];
    }else if (view.tag == 902){
        
        RecordViewController * recordVC = [RecordViewController new];
        
        [self.navigationController pushViewController:recordVC animated:YES];
    }else{
        
        IntroductionViewController * introVC = [IntroductionViewController new];
        introVC.titleStr = @"Introduction";
        introVC.valueStr = @"This is an easy and simple leisure game. \n In the game, you should remember the way home quickly, because the street lights will be off soon, there are many different levels of difficulty, come to challenge together!";
        [self.navigationController pushViewController:introVC animated:YES];
    }

}


- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(40);
        make.top.mas_equalTo(self.view).offset(24);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(35);
        make.bottom.mas_equalTo(self.view).offset(-35);
        make.trailing.mas_equalTo(self.view).offset(-17);
        make.width.mas_equalTo(380);
    }];
    
    [self.howBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-40);
        make.top.mas_equalTo(self.view).offset(28);
        make.width.height.mas_equalTo(44);
    }];
    
    [self.musicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.howBtn.mas_leading).offset(-35);
        make.width.height.centerY.mas_equalTo(self.howBtn);
    }];
    
    [self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(50);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(25);
        make.width.mas_equalTo(196);
        make.height.mas_equalTo(48);
    }];
    
    [self.recordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.playView);
        make.top.mas_equalTo(self.playView.mas_bottom).offset(15);
    }];
    
    [self.instroView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.recordView);
        make.top.mas_equalTo(self.recordView.mas_bottom).offset(15);
    }];
}


- (UIView *)setupViewWithTitle:(NSString *)title WithImage:(NSString *)img WithIndex:(NSInteger)index{
    
    UIView * view = [UIView new];
    
    view.tag = index;
    view.userInteractionEnabled = YES;
    view.backgroundColor = [UIColor colorWithHexString:@"0AB966"];
    
    UIImageView * leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 38, 38)];
    leftImageView.backgroundColor = ZRWhiteColor;
    leftImageView.image = [UIImage imageNamed:img];
    
    [view addSubview:leftImageView];
    
    UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(leftImageView.right + 15, 10, 130, 28)];
    titleLab.text = title;
    titleLab.textColor = ZRWhiteColor;
    titleLab.font = [UIFont boldSystemFontOfSize:20];
    
    [view addSubview:titleLab];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [view addGestureRecognizer:tapGR];
    
    return view;
}

-(UIButton *)setupRightTopButtonWith:(NSString *)img{
    
    UIButton * button = [UIButton new];
    
    [button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickRightTopButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


@end
